#include <iostream>
#include <string>
#include <stack>
using namespace std;
class Solution {
    public:
        void print_num(stack<long long> st) {
            stack<long long> tmp;
            while (!st.empty()) {
                tmp.push(st.top());
                st.pop();
            }
            while (!tmp.empty()) {
                cout << tmp.top() << " ";
                st.push(tmp.top());
                tmp.pop();
            }
            cout << endl;
        }

        void print_op(stack<char> st) {
            stack<char> tmp;
            while (!st.empty()) {
                tmp.push(st.top());
                st.pop();
            }
            while (!tmp.empty()) {
                cout << tmp.top() << " ";
                st.push(tmp.top());
                tmp.pop();
            }
            cout << endl;
        }

        long long _power(long long x, int e) {
            if (e == 0) return 1;
            long long res = x;
            cout << e << endl;
            while (--e) res *= x;
            cout << res << endl;
            return res;
        }

        void _store_num(stack<long long>& st, string& num) {
            if (!num.empty()) {
                st.push(_trans(num));
                last_sym = true;
            }
            num = "";
        }

        long long _trans(string num) {
            long long res = 0;
            auto n = num.size();
            for (size_t i = 0; i < num.size(); ++i) {
                long long cur = num[i] - '0';
                cur *= _power(10, --n);
                res += cur;
            }
            return res;
        }

        int calculate(string s) {
            stack<char> op;
            stack<long long> num;
            string con_num;
            for (auto e : s) {
                // print_num(num);
                // print_op(op);
                // cout << "-----------------" << endl;
                if (e == ' ') continue;
                else if (e == '+' || e == '-') {
                    _store_num(num, con_num);
                    if (op.empty() || op.top() == '(') op.push(e);
                    else if (op.top() == '+' || op.top() == '-') {
                        auto cur_op = op.top();
                        op.pop();
                        auto num_2 = num.top();
                        num.pop();
                        auto num_1 = num.top();
                        num.pop();
                        if (cur_op == '+') num.push(num_1 + num_2);
                        else num.push(num_1 - num_2); 
                        op.push(e);
                    }
                    if (!last_sym) num.push(0);
                    last_sym = false;
                }
                else if (e == '(') {
                    op.push(e);
                }
                else if (e == ')') {
                    _store_num(num, con_num);
                    while (op.top() != '(') {
                        auto t = op.top();
                        op.pop();
                        auto num2 = num.top();
                        num.pop();
                        auto num1 = num.top();
                        num.pop();
                        if (t == '+') num.push(num1 + num2);
                        else num.push(num1 - num2); 
                    }
                    op.pop();
                }
                else con_num += e;
            }
            if (!con_num.empty()) _store_num(num, con_num);

            while (!op.empty()) {
                auto t = op.top();
                op.pop();
                auto num2 = num.top();
                num.pop();
                auto num1 = num.top();
                num.pop();
                if (t == '+') num.push(num1 + num2);
                else num.push(num1 - num2); 
            }
            return num.top();
        }
        bool last_sym = false;
    };
int main(int argc, const char** argv) {
    Solution s;
    cout << s.calculate(" 2-1 + 2 ") << endl;
    return 0;
}